package co.fitstart.coach.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Result;

import co.fitstart.coach.dao.CourseRoundDao;
import co.fitstart.coach.dao.RecordDao;
import co.fitstart.coach.handler.ImageHandler;
import co.fitstart.coach.service.ResultReportService;
import co.fitstart.entity.course.ResultReport;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.CourseRoundResult;
import co.fitstart.entity.user.bwh.BwhRecord;

/**
 * 成果报告
 * @author haiyan_xiao
 *
 */
@Service("resultReportService")
public class ResultReportServiceImpl implements ResultReportService {

    @Dynamic
    private RecordDao recordDao;
    
    @Dynamic
    private CourseRoundDao courseRoundDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
    private ImageHandler imageHandler;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#listBwhRecord(co.fitstart.entity.course.round.CourseRound)
     */
    public Map<Integer, BwhRecord> listBwhRecord(CourseRound courseRound) {
        List<BwhRecord> bwhRecords = recordDao.listBwhRecord(courseRound);
        
        Map<Integer, BwhRecord> result = new HashMap<>();
        
        if(!bwhRecords.isEmpty()) {
            for(BwhRecord bwhRecord : bwhRecords) {
                result.put(bwhRecord.getWeek(), bwhRecord);
            }
        }
        
        return result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#loadResultReport(co.fitstart.entity.course.round.CourseRound)
     */
    public ResultReport loadResultReport(CourseRound courseRound) {
        return genericDaoSupport.searchForObject("FROM ResultReport WHERE disabled = false"
                + " AND courseRound = :courseRound", "courseRound", courseRound, ResultReport.class);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#loadBwhRecord(co.fitstart.entity.course.round.CourseRound, int)
     */
    public BwhRecord loadBwhRecord(CourseRound courseRound, int week) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("courseRound", courseRound);
        parameters.put("week", week);
        
        return genericDaoSupport.searchForObject("FROM BwhRecord WHERE disabled = false"
                + " AND courseRound = :courseRound AND week = :week", parameters, BwhRecord.class);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#editBwhRecord(co.fitstart.entity.course.round.CourseRound, co.fitstart.entity.user.bwh.BwhRecord)
     */
    public void editBwhRecord(CourseRound courseRound, BwhRecord bwhRecord) {
        BwhRecord originalBwhRecord = this.loadBwhRecord(courseRound, bwhRecord.getWeek());
        
        if(originalBwhRecord == null) {
            genericDaoSupport.save(bwhRecord.initilization(courseRound, courseRound.getCoach()));
        } else {
            genericDaoSupport.save(originalBwhRecord.edit(bwhRecord, courseRound.getCoach().getId()));
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#editChange(co.fitstart.entity.course.round.CourseRound, co.fitstart.entity.course.ResultReport)
     */
    public void editChange(CourseRound courseRound, ResultReport resultReport) {
        ResultReport originalResultReport = this.loadResultReport(courseRound);
        
        if(originalResultReport == null) {
            genericDaoSupport.save(resultReport.initilization(courseRound));
        } else {
            genericDaoSupport.update(originalResultReport.editChange(resultReport));
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#editSummary(co.fitstart.entity.course.round.CourseRound, java.lang.String, java.lang.String)
     */
    public void editSummary(CourseRound courseRound, String summary, String wordsToStudent) {
        ResultReport originalResultReport = loadResultReport(courseRound);
        
        if(originalResultReport == null) {
            originalResultReport = new ResultReport(courseRound);
            genericDaoSupport.save(originalResultReport.editCoachSummary(summary, wordsToStudent));
        } else {
            genericDaoSupport.update(originalResultReport.editCoachSummary(summary, wordsToStudent));
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.ResultReportService#uploadContrastPhoto(co.fitstart.entity.course.round.CourseRound, java.lang.String, java.lang.String)
     */
    public Result uploadContrastPhoto(CourseRound courseRound, String mediaId, String suffix) {
        CourseRoundResult courseRoundResult = courseRoundDao.loadCourseRoundResult(courseRound);
        
        String imageName;
        
        String code = courseRound.getCode() == null ? "" : courseRound.getCode();
        if(courseRoundResult == null) {
            imageName = code + "_" + suffix;
        } else {
            if("before".equals(suffix)) {
                if(courseRoundResult.getPreviousPicture() == null) {
                    imageName = code + "_" + suffix;
                } else if(courseRoundResult.getPreviousVersion() == null) {
                    imageName = code + "_" + suffix + "_1";
                } else {
                    imageName = code + "_" + suffix + "_" + (courseRoundResult.getPreviousVersion() + 1);
                }
            } else {
                if(courseRoundResult.getLaterPicture() == null) {
                    imageName = code + "_" + suffix;
                } else if(courseRoundResult.getLaterVersion() == null) {
                    imageName = code + "_" + suffix + "_1";
                } else {
                    imageName = code + "_" + suffix + "_" + (courseRoundResult.getLaterVersion() + 1);
                }
            }
        }
        
        Result result = imageHandler.saveImage(mediaId, imageName);
        
        if("0".equals(result.getCode())) {
            String fileName = (String)result.getData().get("fileName");
            
            if(courseRoundResult == null) {
                courseRoundResult = new CourseRoundResult(courseRound, fileName, suffix);
                genericDaoSupport.save(courseRoundResult);
            } else {
                genericDaoSupport.update(courseRoundResult.editPicture(fileName, suffix));
            }
        }
        
        return result;
    }
}
